package com.pic.mgt.syushin.action;

import java.io.File;

import org.springframework.dao.DataAccessException;

import com.pic.mgt.IgxStatus;
import com.pic.mgt.dao.syushin.ISyushinBusinessMgtDao;
import com.pic.mgt.dao.syushin.SyushinBusinessMgtInfoBean;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.monitor.backup.UnitBackup;
import com.pic.mgt.syushin.SyushinBusinessUnitFactory;
import com.pic.mgt.unit.Business;
import com.pic.mgt.unit.operation.BusinessUnitOperation;
import com.pic.mgt.unit.operation.UnitOperationException;

public class SyushinBusinessResendActionImpl implements
		SyushinBusinessResendAction {
	
	private ISyushinBusinessMgtDao syushinBusinessMgtDao;
	private UnitBackup businessUnitBackup;
	private BusinessUnitOperation businessUnitOperation;
	private String syushinInboxPathStr;
	private File syushinInboxPath;

	@Override
	public boolean resendBusiness(String targetFilename) {
		if (null == targetFilename || targetFilename.isEmpty()) {
			// ********************** Log **********************
			IgxLog.messageLog.warn(this,
							"Resending business failed. Did not provide target file name.");
			// ********************** Log **********************
			
			return false;
		}
		
		
		// ********************** Log **********************
		IgxLog.messageLog.info(this,
						"Starting resend business '{}' preprocess.",
						targetFilename);
		// ********************** Log **********************
		
		
		// 先從資料庫中找出該業務記錄
		SyushinBusinessMgtInfoBean syushinBusinessMgtInfoBean = null;
		try {
			// throws DataAccessException
			syushinBusinessMgtInfoBean = this.syushinBusinessMgtDao.findSyushinBusinessMgtInfoByUnitName(targetFilename);
		} catch (DataAccessException e2) {
			// ********************** Log **********************
			IgxLog.messageDetailsLog.error(this, e2.getMessage());
			// ********************** Log **********************
			
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
					"SUDA-286 Resending business '{}' preprocess occured a database operation error.",
							targetFilename);
			// ********************** Log **********************
			return false;
		}
		
		
		// 檢查上一次執行狀態是否為完成,完成才可以做重送
		if (null == syushinBusinessMgtInfoBean ||
			(!syushinBusinessMgtInfoBean.getStatus().equals(IgxStatus.BUSINESS_COMPLETED) &&
			 !syushinBusinessMgtInfoBean.getStatus().equals(IgxStatus.BUSINESS_BATCH_TMP))) {
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
							"SUDA-287 Resending syushin business preprocess failed. Because the business '{}' reocrd can't find in database or was not successful execution since last time.",
							targetFilename);
			// ********************** Log **********************
			return false;
		}
		
		// 取得在備份路徑下的業務檔案出來(包含確認檔)
		
		// 取得備份路徑
		File businessBackupPath = this.businessUnitBackup.
											getBackupUnitPath(syushinBusinessMgtInfoBean.getCreatingTime());
		// 取得備份路徑下的目標檔
		File targetFile = new File(businessBackupPath.getAbsolutePath(),
								   syushinBusinessMgtInfoBean.getTargetFile());
		// 取得備份路徑下的確認檔
		File confirmationFile = new File(businessBackupPath.getAbsolutePath(),
										 syushinBusinessMgtInfoBean.getConfirmationFile());
		
		// 建立出一個只包含有確認檔和目標檔的空業務出來 (給 businessUnitOperation 使用)
		Business business = new Business(null);
		business.setConfirmationFile(confirmationFile);
		business.setTargetFile(targetFile);
		
		// 更新資料庫為將重做的狀態
		try {
			// throws DataAccessException
			this.syushinBusinessMgtDao.updateStatusByUnitName(
													IgxStatus.BUSINESS_REDO,
													syushinBusinessMgtInfoBean.getUnitName());
		} catch (DataAccessException e) {
			// ********************** Log **********************
			IgxLog.messageDetailsLog.error(this, e.getMessage());
			// ********************** Log **********************
			
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
					"SUDA-286 Resending business '{}' preprocess occured a database operation error.",
							targetFilename);
			// ********************** Log **********************
			return false;
		}
		
		// 將業務複製到 syushin inbox 底下
		try {
			this.businessUnitOperation.copyTargetFile(business, this.syushinInboxPath);
			this.businessUnitOperation.copyConfirmationFile(business, this.syushinInboxPath);
		} catch (UnitOperationException e) {
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
					"SUDA-288 Resending business '{}' preprocess failed in copying business file to syushin inbox step.",
					targetFilename);
			// ********************** Log **********************
			
			// 把資料庫狀態復原回原來的完成狀態
			// throws DataAccessException
			try {
				if (syushinBusinessMgtInfoBean.getType().equals(SyushinBusinessUnitFactory.REAL_SYUSHIN)) {
					this.syushinBusinessMgtDao.updateStatusByUnitName(
															IgxStatus.BUSINESS_COMPLETED,
															syushinBusinessMgtInfoBean.getUnitName());
				}
				else if (syushinBusinessMgtInfoBean.getType().equals(SyushinBusinessUnitFactory.BATCH_SYUSHIN)) {
					this.syushinBusinessMgtDao.updateStatusByUnitName(
															IgxStatus.BUSINESS_BATCH_TMP,
															syushinBusinessMgtInfoBean.getUnitName());
				}
			} catch (DataAccessException e1) {
				// ********************** Log **********************
				IgxLog.messageDetailsLog.error(this, e.getMessage());
				// ********************** Log **********************
				
				// ********************** Log **********************
				IgxLog.messageLog.error(this,
						"SUDA-286 Resending business '{}' preprocess occured a database operation error.",
								targetFilename);
				// ********************** Log **********************
			}
			
			return false;
		}
		
		// ********************** Log **********************
		IgxLog.messageLog.info(this,
						"Resending business '{}' preprocess completed.",
						targetFilename);
		// ********************** Log **********************
		
		return true;
	}

	public void setSyushinBusinessMgtDao(
			ISyushinBusinessMgtDao syushinBusinessMgtDao) {
		this.syushinBusinessMgtDao = syushinBusinessMgtDao;
	}

	public void setBusinessUnitBackup(UnitBackup businessUnitBackup) {
		this.businessUnitBackup = businessUnitBackup;
	}

	public void setBusinessUnitOperation(BusinessUnitOperation businessUnitOperation) {
		this.businessUnitOperation = businessUnitOperation;
	}

	public void setSyushinInboxPathStr(String syushinInboxPathStr) {
		this.syushinInboxPathStr = syushinInboxPathStr;
		this.syushinInboxPath = new File(syushinInboxPathStr);
	}
}
